package Test;
import Comp.CompareName;
import Equalable.Equalable;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Comparator;
import Equalable.*;
import Comp.*;

public class PersonJFrame extends JFrame implements ActionListener, ListSelectionListener {
    private File file;
    protected JMenu[] jMenus;
    protected JTable jTable;
    protected DefaultTableModel tableModel;
    protected PersonJPanel personJPanel;
    protected JComboBox<String>[] comboBoxes;
    private static Equalable[] equalables = {new EqualName(), new EqualBirthdate(), new EqualP()};
    private static Comparator[] comparators = {new CompareName(), new CompareBirthdate(), new CompareP()};
    private JPopupMenu popupMenu;


    public PersonJFrame(Person[] pers, PersonJPanel personJPanel){
        super("person对象管理");
        this.setSize(700, 300);
        this.setLocationRelativeTo(null);//将窗口置于中央
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //this.addMenu();//菜单
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);//左右分割窗格
        this.getContentPane().add(splitPane);//框架内容窗格添加

        this.personJPanel = personJPanel;
        splitPane.add(this.personJPanel);//分割窗格，左边加Person对象信息面板
        splitPane.setDividerLocation(120);//设置水平分割的位置
        splitPane.setOneTouchExpandable(true);

        JPanel Panel = new JPanel(new BorderLayout());//右边板布局
        splitPane.add(Panel);
        this.tableModel = new DefaultTableModel();
        String[] title = {"姓名","出身日期","性别","省份","城市"};
        this.tableModel = new DefaultTableModel(title,0);
        if(pers != null){
            for (int i = 0; i < pers.length; i++) {
                tableModel.setRowCount(i+1);
                tableModel.setValueAt(pers[i].getName(),i,0);
                tableModel.setValueAt(pers[i].getData(),i,1);
                tableModel.setValueAt(pers[i].getSex(),i,2);
                tableModel.setValueAt(pers[i].getProvince(),i,3);
                tableModel.setValueAt(pers[i].getCity(),i,4);
            }
        }

        this.jTable = new JTable(this.tableModel);
        jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jTable.setComponentPopupMenu(popupMenu);

        Panel.add(new JScrollPane(this.jTable));
        JPanel cmdPanel = new JPanel();
        Panel.add(cmdPanel,"South");

        String[][] str = {{"添加","删除","删除选中项"},{"查找关键字","排序关键字"},{"姓名","出身日期","省份"}};
        for (int i = 0; i < str[0].length; i++) {
            JButton button = new JButton(str[0][i]);
            button.addActionListener(this);
            cmdPanel.add(button);
        }
        this.comboBoxes = new JComboBox[str[1].length];
        for (int i = 0; i < str[1].length; i++) {
            cmdPanel.add(new JLabel(str[1][i]));
            cmdPanel.add(this.comboBoxes[i] = new JComboBox<>(str[2]));
            this.comboBoxes[i].addActionListener(this);
        }
        this.setVisible(true);
    }
    @Override
    public void valueChanged(ListSelectionEvent e) {
        int i = this.jTable.getRowCount();
        Person per = new Person((String) jTable.getValueAt(i,0),
                (String)jTable.getValueAt(i,1),
                (String)jTable.getValueAt(i,2),
                (String)jTable.getValueAt(i,3),
                (String)jTable.getValueAt(i,4));
        this.personJPanel.set(per);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource() instanceof JButton){
            Person per = null;
            switch (e.getActionCommand()){
                case "添加":
                    if((per = this.personJPanel.get()) != null){
                        int i = tableModel.getRowCount();
                        tableModel.setRowCount(i+1);
                        tableModel.setValueAt(per.getName(),i,0);
                        tableModel.setValueAt(per.getData(),i,1);
                        tableModel.setValueAt(per.getSex(),i,2);
                        tableModel.setValueAt(per.getProvince(),i,3);
                        tableModel.setValueAt(per.getCity(),i,4);
                    }
                    break;
                case "删除":
                    remove(this.personJPanel.get());
                    break;
                case "删除选中项":
                    this.removeSelected(this.tableModel);
            }
        }else if(e.getSource() instanceof JComboBox){
            if(e.getSource() == this.comboBoxes[0]){
                int i = this.comboBoxes[0].getSelectedIndex();
                selectALL(this.tableModel, this.jTable, this.personJPanel.get(), equalables[i]);
            }else if(e.getSource() == this.comboBoxes[1]){
                int i = this.comboBoxes[1].getSelectedIndex();
                sort(this.tableModel, comparators[i]);
            }
        }
    }
    private void remove(Person obj) {
        if(obj != null && JOptionPane.showConfirmDialog(this,"删除"+obj.toString()+"? ",
                "确认",JOptionPane.YES_NO_OPTION) == 0){
            int j = -1;
            for (int i = 0; i < tableModel.getRowCount(); i++) {
                if(obj.name.equals(tableModel.getValueAt(i,0)) && obj.data.equals(tableModel.getValueAt(i,1))
                && obj.sex.equals(tableModel.getValueAt(i,2)) && obj.province.equals(tableModel.getValueAt(i,3))
                && obj.city.equals(tableModel.getValueAt(i,4))){
                    j = i;
                    break;
                }
            }
            this.tableModel.removeRow(j);
            JOptionPane.showMessageDialog(this,j != -1?"删除成功":"查找不成功，没有删除");
        }
    }
    public void removeSelected(DefaultTableModel defaultTableModel){
        if(this.tableModel.getRowCount() == 0){
            JOptionPane.showMessageDialog(this,"表格为空，不能删除");
        }else{
            int i = this.jTable.getSelectedRow();
            if(i == -1){
                JOptionPane.showMessageDialog(this,"请选中列表框数据");
            }else {
                int k = this.jTable.getSelectedRow() + 1;
                if(JOptionPane.showConfirmDialog(this,"删除第"+k+"行?",
                        "确认",JOptionPane.YES_NO_OPTION)==0){
                    this.tableModel.removeRow(i);
                }
            }
        }
    }
    public void selectALL(DefaultTableModel defaultTableModel, JTable jTable,
                              Person key, Equalable eq){
        int n = defaultTableModel.getRowCount();
        for (int i = 0; i < n; i++) {
            Person person = new Person((String) tableModel.getValueAt(i,0),
                    (String)tableModel.getValueAt(i,1),
                    (String)tableModel.getValueAt(i,2),
                    (String)tableModel.getValueAt(i,3),
                    (String)tableModel.getValueAt(i,4));
            if(eq.equals(key, person)){
                jTable.addColumnSelectionInterval(i,i);//
            }
        }
    }
    public void sort(DefaultTableModel tableModel, Comparator c){
        for (int i = 0; i < tableModel.getRowCount() - 1; i++) {
            int min = i;
            for (int j = i + 1; j < tableModel.getRowCount(); j++) {
                Person person1 = new Person((String) tableModel.getValueAt(j,0),
                        (String)tableModel.getValueAt(j,1),
                        (String)tableModel.getValueAt(j,2),
                        (String)tableModel.getValueAt(j,3),
                        (String)tableModel.getValueAt(j,4));
                Person person2 = new Person((String) tableModel.getValueAt(i,0),
                        (String)tableModel.getValueAt(min,1),
                        (String)tableModel.getValueAt(min,2),
                        (String)tableModel.getValueAt(min,3),
                        (String)tableModel.getValueAt(min,4));
                if(c.compare(person1,person2)<0){
                    min = j;
                }
                if(min != i){
                    for (int k = 0; k < 5; k++) {
                        String tmp = (String) tableModel.getValueAt(i,k);
                        tableModel.setValueAt(tableModel.getValueAt(min,k),i,k);
                        tableModel.setValueAt(tmp,min,k);
                    }
                }
            }
        }
    }

}

